﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HardwareInterface;

namespace login
{
    public partial class FingerForm : Form
    {
        private String uimstring;
        private String srmlstring;
        private FingerScanApp ap;
        private byte[] imgarr;
        private Bitmap bmp;
        private int scantimes;
        private bool flag;
        private UserInfoModel uim;
        
        public FingerForm(String uimstring, String srmlstring)
        {
            this.uimstring = uimstring;
            this.srmlstring = srmlstring;
            this.uim = new UserInfoModel();
            this.uim = this.uim.JsonToEntity(uimstring);
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;

            ap = new FingerScanApp();
            int init = ap.InitDevice();
            if (init == 0)
            { }  //连接失败
            else if (init == -1)
            { }  //打开设备失败
            imgarr = new byte[512];
            bmp = new Bitmap(255, 288);
            scantimes = 1;
            flag = true;
        }


        private void buttonstart_Click(object sender, EventArgs e)
        {
            if(!flag){
                MessageBox.Show("点错按钮啦");
                return;
            }
            picturefinger2.Image = null;
            ap.RegisterFinger_Part1(ref bmp);
            picturefinger1.Image = bmp;
            label1.Text = "请点击再次输入相同指纹以再次输入第" + scantimes + "个手指指纹";
            flag = false;
        }

        private void buttonagain_Click(object sender, EventArgs e)
        {
            if(flag){
                MessageBox.Show("点错按钮啦");
                return;
            }
            flag = true;
            int size = 0;
            int res = ap.RegisterFinger_Part2(ref imgarr, ref size, ref bmp);
            if (res == 0)
            {
                label1.Text = "输入失败，请重新点击开始输入指纹以输入第" + scantimes + "个手指指纹";
                return;
            }
            picturefinger2.Image = bmp;

            //MemoryStream ms = new MemoryStream();
            //bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            if(scantimes == 1){
                //uim.IndexFinger = new Byte[ms.Length];
                //uim.IndexFinger = ms.ToArray();
                //uim.IndexFinger = new byte[imgarr.Length];
                uim.IndexFinger = (byte[])imgarr.Clone();
            }else if (scantimes == 2){
                //uim.MiddleFinger = new Byte[ms.Length];
                //uim.MiddleFinger = ms.ToArray();
                //uim.MiddleFinger = new byte[imgarr.Length];
                uim.MiddleFinger = (byte[])imgarr.Clone();
            }else{
                //uim.RingFinger = new Byte[ms.Length];
                //uim.RingFinger = ms.ToArray();
                //uim.RingFinger = new byte[imgarr.Length];
                uim.RingFinger = (byte[])imgarr.Clone();
            }
            //ms.Close();

            ++scantimes;
            if (scantimes <= 3)
            {
                label1.Text = "请点击开始输入指纹以输入第" + scantimes + "个手指指纹";
            }else{
                label1.Text = "结束输入";
                buttonstart.Hide();
                buttonagain.Hide();
            }
        }

        private void buttonconfirm_Click(object sender, EventArgs e)
        {
            ap.ReleaseDevice();
            uimstring = uim.EntityToJson(uim);
            DBRepository dbr = new DBRepository();
            if (dbr.adduser(uimstring, srmlstring))
            {
                MessageBox.Show("ok");
            }

            //RegisterConfirmForm registerconfirmform = new RegisterConfirmForm(uimstring, srmlstring);
            //this.Hide();
            //registerconfirmform.Show();
        }

        private void buttonback_Click(object sender, EventArgs e)
        {
            TakePhotoForm takephotoform = new TakePhotoForm(uimstring, srmlstring);
            this.Hide();
            takephotoform.Show();
        }


    }
}
